package com.tnl.app.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.tnl.app.entity.Opinion;
import com.tnl.base.BaseDAO;
import com.tnl.utils.DateUtil;


@Repository
public class OpinionDAO {
	private static final String SQL_GET_ID = "select * from tb_opinion  where id = ?";
	private static final String SQL_GET_OPINION_OPNID = "select * from tb_opinion  where openid = ?";
	private static final String SQL_GET_ALL = "select * from tb_opinion ";
	private static final String SQL_SAVE="INSERT INTO tb_opinion(hunterId,title, desciption,openid,createdate) VALUES(?,?,?,?,?)";
	private static final String SQL_GET_OPTION_CREATEDATE="select * from tb_opinion where openid = ? and to_days(createdate) = to_days(now()) limit 0,1;";
	
	public List<Opinion> getALL() {

		return BaseDAO.queryForList(Opinion.class, SQL_GET_ALL);

	}
	
	public int save(Opinion opinion) {
		if (opinion == null) {
			return -1;
		}
		Long result =BaseDAO.save(SQL_SAVE,opinion.getHunterId(),opinion.getTitle(), opinion.getDesciption(),opinion.getOpenid(),DateUtil.currentTime());
		if (result != null) {
			return result.intValue();
		}
		return -1;
	}
	
	public Opinion getById(int Id) {
		return  BaseDAO.queryForObject(Opinion.class, SQL_GET_ID, Id);
	}
	public Opinion getOpinionByOpenId(String openid){
		return  BaseDAO.queryForObject(Opinion.class, SQL_GET_OPINION_OPNID, openid);
	}
	
	/**
	 * get option
	 * @param openid
	 * @return
	 */
	public Opinion getOptionByCreatedate(String openid){
		return BaseDAO.queryForObject(Opinion.class, SQL_GET_OPTION_CREATEDATE,openid);
	}
	
}
